package com.lzy.cloud.payment.dtos.payment;

import com.lzy.cloud.payment.constants.BusinessStatus;
import com.lzy.cloud.payment.constants.CommonConstants;
import com.lzy.cloud.payment.dtos.pay.*;
import com.lzy.cloud.payment.entity.PayInfo;
import com.lzy.cloud.payment.utils.Common;
import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Data
public class PayInfoDto implements Serializable {
    private static final long serialVersionUID = -2356220898271464702L;
    /**
     * 自增ID
     */
    private Long id;

    /**
     * 本地订单号
     */
    private String serialNumber;

    /**
     * 第三方平台订单号
     */
    private String thirdNumber;

    /**
     * 类型 默认0,0：订单支付1：充值
     */
    private Integer type;

    /**
     * 用户ID
     */
    private Long memberId;

    /**
     * 交易状态(0:等待付款  1:支付成功 2:支付失败)
     */
    private Integer status;

    /**
     * 订单金额
     */
    private BigDecimal amount;

    /**
     * 交易手续费
     */
    private BigDecimal fee;

    /**
     * 卖家实际收入金额(减去集分宝,卡券，优惠券抵扣,手续费)
     */
    private BigDecimal receiptAmount;

    /**
     * 更新时间
     */
    private String updateTime;

    /**
     * 支付方式(0-支付宝，1-微信，2-银行卡，3-平台支付)
     */
    private Integer way;

    /**
     * 买家支付成功时间
     */
    private String successTime;

    /**
     * 生成时间
     */
    private String createTime;

    /**
     * 龙珠
     */
    private BigDecimal integration;

    /**
     * 余额支付
     */
    private BigDecimal balancePaid;

    /**
     * 买家实际支付金额(减去集分宝、卡券、优惠券抵扣、龙珠、余额抵扣)
     */
    private BigDecimal payAmount;

    /**
     * 目标类型(0:APP支付 1:WEB支付(小程序支付))
     */
    private Integer targetType;

    /**
     * 支付流水
     *
     * @param basePayDTO
     * @return
     */
    public static Map<String, Object> payInfo(BasePayDTO basePayDTO) {
        Map<String, Object> result = new HashMap<String, Object>();
        switch (basePayDTO.getWay()) {
            case 0:
                AlipayPayDTO apDTO = (AlipayPayDTO) basePayDTO;
                result = AlipayPayInfo(apDTO);
                break;
            case 1:
                WeixinPayDTO wxDTO = (WeixinPayDTO) basePayDTO;
                result = WeixinPayInfo(wxDTO);
                break;
            default:
                break;
        }
        return result;
    }

    public static Map<String, Object> AlipayPayInfo(AlipayPayDTO apDTO) {
        Map<String, Object> map = new HashMap<String, Object>();
        //保存支付信息
        PayInfoDto pib = new PayInfoDto();
        pib.setThirdNumber(apDTO.getTrade_no());
        pib.setStatus(CommonConstants.OrderPayStatus.YESPAY.getIndex());
        pib.setWay(CommonConstants.PayWay.alipay.getIndex());
        pib.setType(apDTO.getType());
        pib.setFee(Common.getTradeFee(BusinessStatus.PayWay.alipay, apDTO.getTotal_amount()));
        if (apDTO.getGmt_payment() != null) pib.setSuccessTime(apDTO.getGmt_payment());   //支付时间
        if (apDTO.getReceipt_amount() != null)
            pib.setReceiptAmount(BigDecimal.valueOf(Double.parseDouble(apDTO.getReceipt_amount())));  //用户实付金额
        map.put("payInfoDto", pib);

        //保存支付详细信息
        PayInfoDetailDto pid = new PayInfoDetailDto();
        pid.setBuyerId(apDTO.getBuyer_id());
        pid.setBuyerLogonId(apDTO.getBuyer_logon_id());
        pid.setSellerId(apDTO.getSeller_id());
        pid.setSellerEmail(apDTO.getSeller_email());
        map.put("payInfoDetailDto", pid);
        return map;
    }

    //微信
    public static Map<String, Object> WeixinPayInfo(WeixinPayDTO wxDTO) {
        Map<String, Object> map = new HashMap<String, Object>();
        //保存支付信息
        PayInfoDto pib = new PayInfoDto();
        pib.setThirdNumber(wxDTO.getTransaction_id());
        pib.setStatus(CommonConstants.OrderPayStatus.YESPAY.getIndex());
        pib.setReceiptAmount(null);
        pib.setWay(CommonConstants.PayWay.weixin.getIndex());
        pib.setType(wxDTO.getType());
        pib.setFee(Common.getTradeFee(BusinessStatus.PayWay.weixin, wxDTO.getTotal_fee()));
        pib.setSuccessTime(wxDTO.getTime_end());
        map.put("payInfoDto", pib);

        //保存支付详细信息
        PayInfoDetailDto pidDO = new PayInfoDetailDto();
        pidDO.setOpenid(wxDTO.getOpenid());
        pidDO.setBankCode(wxDTO.getBank_type());
        map.put("payInfoDetailDto", pidDO);
        return map;
    }

    /**
     * 生成预付订单  ---公共支付
     *
     * @param commonPayDTO
     * @return
     */
    public static PayInfoDto commonPayConvert(CommonPayDTO commonPayDTO) {
        PayInfoDto payInfoDO = new PayInfoDto();
        payInfoDO.setSerialNumber(commonPayDTO.getSerialNumber());
        payInfoDO.setType(commonPayDTO.getType());
        payInfoDO.setStatus(CommonConstants.OrderPayStatus.NOPAY.getIndex());
        payInfoDO.setMemberId(commonPayDTO.getUserId());
        payInfoDO.setAmount(commonPayDTO.getAmount());
        payInfoDO.setWay(commonPayDTO.getWay());
        payInfoDO.setTargetType(CommonConstants.PayTargetType.APP.getIndex());
        return payInfoDO;
    }

    /**
     * 生成预付支付信息(支付宝/微信在线支付)
     *
     * @param mrDTO
     * @return
     */
    public static PayInfoDto convert(MobileRequestDTO mrDTO) {
        PayInfoDto payInfoDO = new PayInfoDto();
        payInfoDO.setSerialNumber(mrDTO.getOrderId());
        payInfoDO.setStatus(CommonConstants.OrderPayStatus.NOPAY.getIndex());
        payInfoDO.setTargetType(CommonConstants.PayTargetType.APP.getIndex());
        //在线支付
        if (mrDTO.getRequestType() == BusinessStatus.BusinessType.prepayOrder) {
            payInfoDO.setType(mrDTO.getType());
            payInfoDO.setMemberId(mrDTO.getPayOrderDTO().getMemberId());
            payInfoDO.setAmount(mrDTO.getPayOrderDTO().getOrderAmount());
            payInfoDO.setWay(mrDTO.getWay());
        }
        return payInfoDO;
    }

    /**
     * 更新
     *
     * @return
     */
    public PayInfo converts() {
        PayInfo payInfo = new PayInfo();
        payInfo.setId(id);
        payInfo.setThirdNumber(this.thirdNumber);
        payInfo.setStatus(this.status);
        payInfo.setWay(this.way);
        payInfo.setFee(this.fee);
        payInfo.setReceiptAmount(this.receiptAmount);
        payInfo.setSuccessTime(this.successTime);
        payInfo.setPayAmount(this.payAmount);
        return payInfo;
    }
}
